import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from "./store"
import qs from 'qs'
import "../src/assets/iconfont/iconfont.css"

import { postRequest } from './utils/api'
import { getRequest } from './utils/api'
import { putRequest } from './utils/api'
import { deleteRequest } from './utils/api'
import { initMenu } from './utils/menu'

Vue.config.productionTip = false
Vue.prototype.$qs=qs
Vue.use(ElementUI)

//插件形式引入请求
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;

router.beforeEach((to, from, next) => {
  if (window.sessionStorage.getItem('tokenStr')) {
    initMenu(router, store);
    if (!window.sessionStorage.getItem('user')) {
      return getRequest('/user/info').then(resp => {
        if (resp) {
          window.sessionStorage.setItem('user', JSON.stringify(resp));
          next();
        }
      })
    }
    next();
  } else {
    if (to.path == '/' || to.path == '/register') {
      next();
    } else {
      next('/?redirect=' + to.path);
    }
  }
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
